﻿using System;
using System.Reflection;
using System.Runtime.Serialization;

namespace MugenMvvmToolkit.Models
{
    /// <summary>
    ///     Represents the class which contains information about weak event.
    /// </summary>
#if WPF
    [Serializable]
#endif
    [DataContract]
    public sealed class WeakEventInfo
    {
        #region Const

#if NETFX_CORE
        /// <summary>
        /// Gets the information about a Target field.
        /// </summary>
        public static readonly FieldInfo TargetFieldInfo = typeof(WeakEventInfo).GetTypeInfo().GetDeclaredField("Target");

        /// <summary>
        /// Gets the information about a UnsubcribeDelegate field.
        /// </summary>
        public static readonly FieldInfo UnsubcribeDelegateFieldInfo =
            typeof(WeakEventInfo).GetTypeInfo().GetDeclaredField("UnsubcribeDelegate");

        /// <summary>
        /// Gets the information about a Delegate field.
        /// </summary>
        public static readonly FieldInfo DelegateFieldInfo = typeof(WeakEventInfo).GetTypeInfo().GetDeclaredField("Delegate");

        /// <summary>
        /// Gets the information about a Clear method.
        /// </summary>
        public static readonly MethodInfo ClearMethodInfo = typeof(WeakEventInfo).GetTypeInfo().GetDeclaredMethod("Clear");
#else

        /// <summary>
        ///     Gets the information about a Target field.
        /// </summary>
        public static readonly FieldInfo TargetFieldInfo = typeof(WeakEventInfo).GetField("Target");

        /// <summary>
        ///     Gets the information about a UnsubcribeDelegate field.
        /// </summary>
        public static readonly FieldInfo UnsubcribeDelegateFieldInfo =
            typeof(WeakEventInfo).GetField("UnsubcribeDelegate");

        /// <summary>
        ///     Gets the information about a Delegate field.
        /// </summary>
        public static readonly FieldInfo DelegateFieldInfo = typeof(WeakEventInfo).GetField("Delegate");

        /// <summary>
        ///     Gets the information about a Clear method.
        /// </summary>
        public static readonly MethodInfo ClearMethodInfo = typeof(WeakEventInfo).GetMethod("Clear");
#endif

        #endregion

        #region Fields

        /// <summary>
        ///     Gets or sets the delegate to invoke weak event.
        /// </summary>
        public Delegate Delegate;

        /// <summary>
        ///     Gets the target.
        /// </summary>
        public WeakReference Target;

        /// <summary>
        ///     Gets the unsubcribe delegate if any.
        /// </summary>
        public Action<Delegate> UnsubcribeDelegate;

        /// <summary>
        /// Gets the information about original method.
        /// </summary>
        public MethodInfo OriginalMethod;

        #endregion

        #region Method

        /// <summary>
        ///     Clears all fields.
        /// </summary>
        public void Clear()
        {
            if (UnsubcribeDelegate != null)
                UnsubcribeDelegate(Delegate);
            Delegate = null;
            Target = null;
            UnsubcribeDelegate = null;
            OriginalMethod = null;
        }

        #endregion
    }
}
